package com.android.gufengmall.mall;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.CarouselAdapter;
import com.android.gufengmall.adapter.CarouselTitleAdapter;
import com.android.gufengmall.adapter.MallShopAdapter;
import com.android.gufengmall.bean.CarouselBean;
import com.android.gufengmall.bean.GoodsBean;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.GoodsBeanDao;
import com.android.gufengmall.utils.LogUtils;
import com.android.gufengmall.video.SearchActivity;
import com.google.android.material.snackbar.Snackbar;
import com.youth.banner.Banner;
import com.youth.banner.config.BannerConfig;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.util.BannerUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商城
 */
public class MallFragment extends Fragment implements MallShopAdapter.MallShopListener {

    private EditText mEdtSearch;
    private Banner mBanner;
    private RecyclerView mRvMall;

    private List<GoodsBean> mGoodsBeanList;

    private CarouselAdapter mCarouselAdapter;
    private MallShopAdapter mMallShopAdapter;

    private App mMyApp;
    private DaoSession mDaoSession;
    private GoodsBeanDao mGoodsBeanDao;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_mall, container, false);
        initView(root);
        initData();
        initListener();
        return root;
    }

    private void initListener() {
        mMallShopAdapter.setOnMallShopListener(this);
        mEdtSearch.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), SearchActivity.class);
            intent.putExtra("isVideo", false);
            startActivity(intent);
        });
    }

    private void initData() {
        mGoodsBeanList = new ArrayList<>();

        mMyApp = (App) getActivity().getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mGoodsBeanDao = mDaoSession.getGoodsBeanDao();

        List<GoodsBean> goodsBeans = mGoodsBeanDao.loadAll();
        mGoodsBeanList.addAll(goodsBeans);

        mRvMall.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        mMallShopAdapter = new MallShopAdapter(requireContext(), mGoodsBeanList);
        mRvMall.setAdapter(mMallShopAdapter);

        mCarouselAdapter = new CarouselAdapter(CarouselBean.getCarouselData());

        mBanner.setAdapter(mCarouselAdapter)
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(requireContext()))//设置指示器
                .setOnBannerListener((data, position) -> {
                    Snackbar.make(mBanner, ((CarouselBean) data).title, Snackbar.LENGTH_SHORT).show();
                    LogUtils.d("position：" + position);
                });

        mBanner.setAdapter(new CarouselTitleAdapter(CarouselBean.getMallCarouselData()));
        mBanner.setIndicator(new CircleIndicator(requireContext()));
        mBanner.setIndicatorGravity(IndicatorConfig.Direction.RIGHT);
        mBanner.setIndicatorMargins(new IndicatorConfig.Margins(0, 0,
                BannerConfig.INDICATOR_MARGIN, BannerUtils.dp2px(12)));
    }

    private void initView(View root) {
        mEdtSearch = root.findViewById(R.id.edt_search);
        mBanner = root.findViewById(R.id.banner);
        mRvMall = root.findViewById(R.id.mall_rv);
    }

    @Override
    public void onMallShopClick(String goodsName,int position) {
        Intent intent = new Intent(getActivity(), MallDetailsActivity.class);
        intent.putExtra("goodsName", goodsName);
        intent.putExtra("position", position);
        startActivity(intent);
    }
}